<?php
class jckbrowserActions extends sfActions
{

  /**
   * Supprime le fichier
   *
   *
   */
  public function executeDelete( sfWebRequest $request )
  {
    // Récupération du profil et validation
    // des droits d'accès
    $this->checkProfil($request);


    if ( !$this->profilConfiguration->allowDelete() ){
      $this->forwardToSecureDestination();
    }

    // Control CSRF
    $request->checkCSRFProtection();

    try{
      $file = new FileSystemEntry($this->profilConfiguration->getPath().'/'.$request->getParameter('file'));
      if( $file->isFile() ){
        $file->delete();
        $this->getUser()->setFlash('notice', sprintf(__("File '%s' deleted success",null,'jckckeditor'), $file));

      } else {
        $this->getUser()->setFlash('error', sprintf(__("File '%s' can't be deleted, it's not a file.",null,'jckckeditor'), $file));
      }
    } catch ( FileSystemManagerException $e ){
      $this->getUser()->setFlash('error', sprintf(__($e->getMessage(),null,'jckckeditor'), $request->getParameter('file')));
    }

    $this->redirect($request->getReferer());
  }



  /**
   * Upload le fichier transmis.
   * 
   * @param sfWebRequest $request
   */
  public function executeUpload( sfWebRequest $request)
  {
    $this->checkProfil($request);

    if ( !$this->profilConfiguration->allowUpload() ){
      $this->redirect(sfConfig::get('app_jckbrowser_secure_route'), sfConfig::get('app_jckbrowser_secure_code'));
    }

    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $form = new JckBrowserUploadForm(null, array('upload_path'=>$this->profilConfiguration->getPath()));

    if ( $request->isMethod(sfRequest::POST) ) {
      
      //$request->checkCSRFProtection();
      $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

      // Form OK
      if ($form->isValid()) {
        
        $file = $form->getValue('file');
        $originalName = $file->getOriginalName();

        // File already exist
        if( file_exists($file->getPath().'/'.$originalName) ) {
          $this->getUser()->setFlash('error', sprintf(__("File '%s' already exist",null,'jckckeditor'), $originalName));
        }

        // No, save the file
        else {
          $file->save($file->getPath().'/'.$originalName); 
          $this->getUser()->setFlash('notice', sprintf(__("File '%s' uploaded success",null,'jckckeditor'), $originalName));
        }
      } 

      // Form KO
      else {
        $this->getUser()->setFlash('error', __("Invalid data !",null,'jckckeditor'));
      }
    }
    $this->redirect($request->getReferer());
  }



  /**
   * Affichage du Browser de fichier.
   *
   * @param sfWebRequest $request
   */
  public function executeBrowse( sfWebRequest $request)
  {
    $this->checkProfil($request);

    if( $this->profilConfiguration->allowUpload() ) {
      $this->form = new JckBrowserUploadForm(null, array('upload_path'=>$this->profilConfiguration->getPath()));
    }
    try {
      $this->dir = FileSystemManager::parseDirectory($this->profilConfiguration->getPath());
     } catch ( FileSystemManagerException $e ){
       $this->dir = array();
      $this->getUser()->setFlash('error', sprintf(__($e->getMessage(),null,'jckckeditor'), $request->getParameter('file')));
    }
    
    $this->setLayout(false);
  }


  
  protected function checkProfil(sfWebRequest $request)
  {
    sfApplicationConfiguration::getActive()->loadHelpers('I18N');

    $this->profilConfiguration = new JckBrowserProfil($request->getParameter('profil'));

  }



  protected function forwardToSecureDestination()
  {
    $this->redirect(sfConfig::get('app_jckbrowser_secure_route'), sfConfig::get('app_jckbrowser_secure_code'));
  }
}